package com.ruoyi.jiajia.controller;

import com.github.pagehelper.ISelect;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.JiajiaItem;
import com.ruoyi.system.domain.JiajiaItemSort;
import com.ruoyi.system.service.IJiajiaItemService;
import com.ruoyi.system.service.IJiajiaItemSortService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 项目类别Controller
 * 
 * @author mrwang
 * @date 2024-04-19
 */
@Controller
@RequestMapping("/jiajia/item/sort")
public class JiajiaItemSortController extends BaseController
{
    private String prefix = "jiajia/item/sort";

    @Autowired
    private IJiajiaItemSortService jiajiaItemSortService;



    @RequiresPermissions("jiajia:itemsort:view")
    @GetMapping()
    public String item()
    {
        return prefix + "/item";
    }

    /**
     * 查询项目列表
     */
    @RequiresPermissions("jiajia:itemsort:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(JiajiaItemSort jiajiaItemSort)
    {
        startPage();
        List<JiajiaItemSort> list = jiajiaItemSortService.selectJiajiaItemSortList(jiajiaItemSort);
        return getDataTable(list);
    }

    /**
     * 导出项目列表
     */
    @RequiresPermissions("jiajia:itemsort:export")
    @Log(title = "项目类别", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(JiajiaItemSort jiajiaItemSort)
    {
        List<JiajiaItemSort> list = jiajiaItemSortService.selectJiajiaItemSortList(jiajiaItemSort);
        ExcelUtil<JiajiaItemSort> util = new ExcelUtil<JiajiaItemSort>(JiajiaItemSort.class);
        return util.exportExcel(list, "项目类别数据");
    }

    /**
     * 新增项目
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存项目
     */
    @RequiresPermissions("jiajia:itemsort:add")
    @Log(title = "新增项目类别", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(JiajiaItemSort jiajiaItemSort)
    {
        if(StringUtils.isEmpty(jiajiaItemSort.getName())){
            return AjaxResult.error("项目类别名称不能为空");
        }
        long count = PageHelper.count(new ISelect() {
            @Override
            public void doSelect() {
                JiajiaItemSort itemSort = new JiajiaItemSort();
                itemSort.getParams().put("name", jiajiaItemSort.getName());
                jiajiaItemSortService.selectJiajiaItemSortList(itemSort);
            }
        });
        if (count > 0){
            return AjaxResult.error("项目类别名称重复");
        }


        return toAjax(jiajiaItemSortService.insertJiajiaItemSort(jiajiaItemSort));
    }

    /**
     * 修改项目
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        JiajiaItemSort jiajiaItemSort = jiajiaItemSortService.selectJiajiaItemSortById(id);
        mmap.put("jiajiaItemSort", jiajiaItemSort);
        return prefix + "/edit";
    }

    /**
     * 修改保存项目
     */
    @RequiresPermissions("jiajia:itemsort:edit")
    @Log(title = "编辑项目类别", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(JiajiaItemSort jiajiaItemSort)
    {

        if(StringUtils.isEmpty(jiajiaItemSort.getName())){
            return AjaxResult.error("项目类别名称不能为空");
        }

        return toAjax(jiajiaItemSortService.updateJiajiaItemSort(jiajiaItemSort));
    }

    /**
     * 删除项目
     */
    @RequiresPermissions("jiajia:itemsort:remove")
    @Log(title = "项目", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(jiajiaItemSortService.deleteJiajiaItemSortByIds(ids));
    }

    /**
     * 项目详情
     */
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Long id, ModelMap mmap) {
        mmap.put("id",id);
        JiajiaItemSort itemSort = jiajiaItemSortService.selectJiajiaItemSortById(id);
        mmap.put("itemSortInfo",itemSort);
        return prefix + "/detail";
    }
}
